package com.xxz.terminal;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "store";
    EditText user_id2,password2,password2_1;
    DBHelper dbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        user_id2 = findViewById(R.id.user_id2);
        password2 = findViewById(R.id.password2);
        password2_1 = findViewById(R.id.password2_1);

        dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();

    }

    public void register2(View btn){

        String user_id = user_id2.getText().toString();
        String password1 = password2.getText().toString();
        String password2 = password2_1.getText().toString();

        if(user_id.equals("") || password1.equals("")){
            Toast.makeText(this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
        }
        else if(password2.equals("")){
            Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
        }
        //如果账号，密码，确认密码均输入
        else{
            Cursor cursor = db.query("userinformation",new String[]{"user_id"},"user_id = ?"
                    , new String[]{user_id},null,null,null);//判断该账号是否存在于数据库中
            if(cursor.getCount() == 0){
                if(password1.equals(password2)){
                    //保存当前账号信息至数据库
                    ContentValues values1 = new ContentValues();
                    String nickname = "你的名字";
                    String motto = "你的句子";
                    values1.put("user_id",user_id);
                    values1.put("password",password1);
                    values1.put("nickname",nickname);
                    values1.put("motto",motto);
                    db.insert("userinformation",null,values1);

                    //保存到Bundle或放入到Extra
                    Intent intent = getIntent();
                    Bundle bdl = new Bundle();
                    bdl.putString("user_id",user_id);
                    bdl.putString("password",password1);

                    intent.putExtras(bdl);
                    setResult(2,intent);

                    //返回到调用页面
                    finish();

                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

                }
                else{
                    Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                Toast.makeText(this, "该账号已注册", Toast.LENGTH_SHORT).show();
            }
        }

    }
}